package com.qianfeng.admin.config;


import com.qianfeng.common.Constants;
import org.apache.shiro.mgt.SecurityManager;
import com.qianfeng.admin.shiro.CustomRealm;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * shiro配置文件
 */
@Configuration
public class ShiroConfig {

    //将realm交给springmvc管理
    @Bean
    public CustomRealm customRealm(){
        return new CustomRealm();
    }

    //配置SecurityManager
    @Bean
    public SecurityManager securityManager(){
        DefaultSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(customRealm());
        return securityManager;
    }

    //配置shiro请求过滤链
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager());
        shiroFilterFactoryBean.setLoginUrl("/login");
        Map<String , String> filterChainDefinitionMap = new LinkedHashMap<>();
        for (String path : Constants.EXCLUDE_PATH){
            //使用过滤器的形式配置请求地址的依赖权限
            filterChainDefinitionMap.put(path, "anon");
        }
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }




}
